package com.example.itemdecorationdemo.fragment

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.example.itemdecorationdemo.R
import com.example.itemdecorationdemo.itemDecoration.*
import com.example.itemdecorationdemo.model.MainViewModel
import com.example.itemdecorationdemo.itemDecoration.SectionDecoration.DecorationCallback

class SimpleDividerFragment : Fragment() {
    private var mViewModel: MainViewModel? = null
    private lateinit var recyclerView: RecyclerView
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.simple_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        // TODO: Use the ViewModel

        Log.d(
            "zhangyu",
            "onActivityCreated mViewModel?.type=${mViewModel?.type} ${arguments?.getInt("type")}"
        )
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerView = view.findViewById(R.id.simple_fragment_rv)
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager




        Log.d(
            "zhangyu",
            "onViewCreated mViewModel?.type=${mViewModel?.type} ${arguments?.getInt("type")}"
        )
        when (arguments?.getInt("type") ?: 0) {
            1 -> recyclerView.addItemDecoration(SimpleDividerDecoration1())
            2 -> recyclerView.addItemDecoration(SimpleDividerDecoration2())
            3 -> recyclerView.addItemDecoration(DividerItemDecoration(requireContext()))
            4 -> recyclerView.addItemDecoration(LeftAndRightTagDecoration(requireContext()))
            5 -> {
                recyclerView.addItemDecoration(DividerItemDecoration(requireContext()))
                recyclerView.addItemDecoration(LeftAndRightTagDecoration(requireContext()))
            }

            6 -> {
                recyclerView.addItemDecoration(PinnedSectionDecorationHead(context, callback))
            }
            7 -> {
                recyclerView.addItemDecoration(PinnedSectionDecoration(context, callback))
            }
            8 -> {
                recyclerView.addItemDecoration(PinnedSectionTestDecoration(context, callback))
            }
            9 -> recyclerView.addItemDecoration(TestItemDecoration(requireActivity()))
            else -> {
            }
        }

        recyclerView.adapter = object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): ViewHolder {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.main_fragment_item, parent, false)
                return ViewHolder(view)
            }

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                holder.title.text = "第" + position + "个item"
            }

            override fun getItemCount(): Int {
                return 101
            }
        }
    }

    //简单分割线
    class SimpleDividerDecoration1 : ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
            outRect.bottom = 10
        }
    }

    class SimpleDividerDecoration2 : ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            outRect[20, 40, 5] = 10
        }
    }

    class DividerItemDecoration(context: Context) : ItemDecoration() {
        private val dividerHeight: Int
        private val dividerPaint: Paint
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            super.getItemOffsets(outRect, view, parent, state)
            outRect.bottom = dividerHeight
        }

        override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
            val childCount = parent.childCount
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            for (i in 0 until childCount - 1) {
                val view = parent.getChildAt(i)
                val top = view.bottom.toFloat()
                val bottom = (view.bottom + dividerHeight).toFloat()
                c.drawRect(left.toFloat(), top, right.toFloat(), bottom, dividerPaint)
            }
        }

        init {
            dividerPaint = Paint()
            dividerPaint.color = context.resources.getColor(R.color.purple_500)
            dividerHeight = context.resources.getDimensionPixelSize(R.dimen.divider_height)
        }
    }

    inner class LeftAndRightTagDecoration(context: Context) : ItemDecoration() {
        private val tagWidth: Int
        private val leftPaint: Paint
        private val rightPaint: Paint
        override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
            super.onDrawOver(c, parent, state)
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)
                val pos = parent.getChildAdapterPosition(child)
                val isLeft = pos % 2 == 0
                if (isLeft) {
                    val left = child.left.toFloat()
                    val right = left + tagWidth
                    val top = child.top.toFloat()
                    val bottom = child.bottom.toFloat()
                    c.drawRect(left, top, right, bottom, leftPaint)
                } else {
                    val right = child.right.toFloat()
                    val left = right - tagWidth
                    val top = child.top.toFloat()
                    val bottom = child.bottom.toFloat()
                    c.drawRect(left, top, right, bottom, rightPaint)
                }
            }
        }

        init {
            leftPaint = Paint()
            leftPaint.color = context.resources.getColor(R.color.purple_500)
            rightPaint = Paint()
            rightPaint.color = context.resources.getColor(R.color.teal_700)
            tagWidth = context.resources.getDimensionPixelSize(R.dimen.tag_width)
        }
    }

    private class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: TextView

        init {
            title = itemView.findViewById(R.id.main_fragment_item_title)
        }
    }

    companion object {
        fun newInstance(): SimpleDividerFragment {
            return SimpleDividerFragment()
        }

        val callback = object : DecorationCallback {
            override fun isGroupHead(position: Int): Boolean {
                return (position % 5) == 0
            }

            override fun getGroupHeadTitle(position: Int): String {
                return "A" + (position / 5).toLong()
            }
        }
    }
}